package com.ice.work.moment.facade.ticket.mapper;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.honeybee.cloud.framework.common.enums.DataStatusEnum;
import com.honeybee.cloud.framework.common.mapper.BaseCovertMapper;
import com.honeybee.cloud.framework.common.page.Page;
import com.ice.work.moment.facade.ticket.constant.SystemConstant;
import com.ice.work.moment.facade.ticket.dto.CarDTO;
import com.ice.work.moment.facade.ticket.dto.PassengerDTO;
import com.ice.work.moment.facade.ticket.enums.TicketCategoryEnum;
import com.ice.work.moment.facade.ticket.model.Cookie;
import com.ice.work.moment.facade.ticket.param.CookieParam;
import com.ice.work.moment.facade.ticket.param.LoginParam;
import com.ice.work.moment.facade.ticket.param.QueryPassengerParam;
import com.ice.work.moment.facade.ticket.param.QueryTicketParam;
import com.ice.work.moment.facade.ticket.request.LoginRequest;
import com.ice.work.moment.facade.ticket.request.QueryTicketRequest;
import com.ice.work.moment.facade.ticket.response.PassengerData;
import com.ice.work.moment.facade.ticket.response.PassengersData;
import com.ice.work.moment.facade.ticket.vo.CarVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Description ParamConvertMapper
 * @Author DELL
 * @Create 2019-01-09
 * @Since 1.0.0
 */
@Mapper
@SuppressWarnings("all")
public interface ParamConvertMapper extends BaseCovertMapper {
    ParamConvertMapper INSTANCE = Mappers.getMapper(ParamConvertMapper.class);

    default boolean toBoolean(String result) {
        return Objects.equals(result, SystemConstant.SUCCESS_FLG) || Objects.equals(String.valueOf(DataStatusEnum.VALID.key), result);
    }


    /**
     * 时间转字符串
     *
     * @param time
     * @return
     */
    @Named("toStr")
    default String toStr(Date time) {
        if (Objects.isNull(time)) {
            return null;
        }
        String result = null;
        try {
            result = DateUtil.format(time, "yyyy-MM-dd");
        } catch (Exception e) {

        }
        return result;
    }

    /**
     * 转车票类型
     *
     * @param code
     * @return
     */
    @Named("toTicketType")
    default String toTicketType(Integer code) {
        return TicketCategoryEnum.getCode(code);
    }

    /**
     * to param
     *
     * @param request
     * @return
     */
    @Mappings({
            @Mapping(source = "loginName", target = "username"),
    })
    LoginParam toParam(LoginRequest request);

    /**
     * to toModel
     *
     * @param param
     * @return
     */
    @Mappings({
            @Mapping(source = "exp", target = "expireAt", qualifiedByName = "toDate"),
            @Mapping(source = "dfp", target = "code")
    })
    Cookie toModel(CookieParam param);

    /**
     * to param
     *
     * @param request
     * @return
     */
    @Mappings({
            @Mapping(source = "ticketType", target = "ticketType", qualifiedByName = "toTicketType"),
            @Mapping(source = "trainDate", target = "trainDate", qualifiedByName = "toStr")
    })
    QueryTicketParam toParam(QueryTicketRequest request);

    /**
     * toVO
     *
     * @param request
     * @return
     */
    @Mappings({
            @Mapping(source = "canBook", target = "canBook", qualifiedByName = "toBoolean"),
            @Mapping(source = "supportCard", target = "supportCard", qualifiedByName = "toBoolean")
    })
    CarVO toVO(CarDTO from);

    /**
     * toVO
     *
     * @param request
     * @return
     */
    List<CarVO> toVO(Collection<CarDTO> from);


    /**
     * to param
     *
     * @param request
     * @return
     */
    @Mappings({
            @Mapping(source = "size", target = "pageSize"),
            @Mapping(source = "current", target = "pageIndex")
    })
    QueryPassengerParam toParam(IPage request);

    /**
     * to param
     *
     * @param request
     * @return
     */
    @Mappings({
            @Mapping(source = "isUserSelf", target = "userSelf", qualifiedByName = "toBoolean")
    })
    PassengerDTO toDTO(PassengerData data);

    /**
     * to dto
     *
     * @param request
     * @return
     */
    List<PassengerDTO> toDTO(Collection<PassengerData> data);

    /**
     * to dto
     *
     * @param request
     * @return
     */
    @Mappings({
            @Mapping(source = "soure.pageTotal", target = "total"),
            @Mapping(source = "soure.data", target = "records")
    })
    Page<PassengerDTO> toDTO(PassengersData soure,Long current);
    /**
     * to dto
     *
     * @param request
     * @return
     */
    @Mappings({
            @Mapping(source = "soure.pageTotal", target = "total"),
            @Mapping(source = "soure.data", target = "records")
    })
    Page<PassengerDTO> toDTO(PassengersData soure,Long current,Long size);
}
